#!/usr/bin/python
#example code, lecture 6

#compute square root of x using bi-section method
#there is a bug in this function, Prof. John Guttag
def squareRootBi( x,epsilon ):
    """Assuse x >= 0 and epsilon > 0
        Return y, y*y is within epsilon of x"""
    assert x >= 0, 'x must be non-negative, not' + str( x )
    assert epsilon > 0, 'epsilon must be postive, no ' + str( epsilon )
    low = 0
    #here is a bug when x less than 1
    #high = x
    high = max( x,1.0 )
    guess = ( low+high )/2.0
    ctr = 1
    trytime = 100

    while abs( guess**2 - x )>epsilon and ctr <= trytime:
        #print 'low:', low, 'high:', high, 'guess,', guess
        if guess**2 < x:
            low = guess
        else:
            high = guess
        guess = ( low + high )/2.0
        ctr += 1
    assert ctr <= trytime, 'Iteration count exceeded'  
    print 'Bi method. Num, iteration:',ctr,'estimate:',guess
    return guess

#squareRootBi(16,0.0001)
#squareRootBi(9,0.0001)
#squareRootBi(0.25,0.0001)

def squareRootNR( x,epsilon ):
    """Return square root of x by Newton's method"""
    assert x >= 0, 'x must be non-negative, not' + str( x )
    assert epsilon > 0, 'epsilon must be postive, no ' + str( epsilon )
    x = float( x )
    guess = x/2.0
    #guess = 0.001

    diff = guess**2-x
    ctr = 1
    trytime = 100

    while abs( diff ) > epsilon and ctr <= trytime:
        print 'Error:', diff, 'guess', guess
        guess = guess - diff/( 2.0*guess )
        diff = guess**2 - x
        ctr += 1
    assert ctr <= trytime, 'Iteration count exceeded'  
    print 'Bi method. Num, iteration:',ctr,'estimate:',guess
    return guess

#squareRootNR( 16,0.0001 )
#squareRootNR( 9,0.0001 )
#squareRootNR( 0.5,0.0001 )

#def compareMethods():
    #print


Techs = [ 'MIT', 'Cal Tech' ]
print Techs
raw_input()
Ivys = [ 'Harvard', 'Yale', 'Brown' ]
print Ivys
raw_input()
Univs = [ ]
#append method
Univs.append( Techs )
print Univs
raw_input()
Univs.append( Ivys )
print Univs
raw_input()

for e in Univs:
    print e
    for c in e: print c
raw_input()

#Flatten list
Univs = Techs + Ivys
print Univs
raw_input()
Ivys.remove( 'Harvard' )
print 'Ivys:', Ivys
print 'Univs:', Univs
